<?php
abstract class GetRank
{
	abstract function execute();

	protected $ranking;
	protected $user_id;
	protected $tactics_id;
	protected $paging;

	function __construct ($user_id,$tactics_id=null,Paging $paging=null)
	{
		$this->ranking = new Ranking();
		$this->user_id = $user_id;
		$this->tactics_id = $tactics_id;
		$this->paging = $paging;
	}

}

//通过策略id显示策略信息
class GetRankViewBytacticsId extends GetRank
{
	/**
	 * [execute description]
	 * @param  [int]      $user_id    [用户id,为了得关键词表]
	 * @param  [int]      $tactics_id [策略id]
	 * @param  Paging     $paging     [分页类]
	 * @return [type]                 [策略信息]
	 */
	function __construct($user_id,$tactics_id,Paging $paging)
	{
		parent::__construct($user_id,$tactics_id,$paging);
	}

	function execute()
	{

		if (!$this->paging) {
			throw new Exception("paging 不能为空", 3);
		}

		if (is_array($this->tactics_id)) {
			foreach ($this->tactics_id as $key) {
				$this->ranking->check_tactics_id($key);
			}
			$tactics_id_qureStr = implode(',',$this->tactics_id);
			$where = "tactics.tactics_id IN ($tactics_id_qureStr)";
		}else{
			$this->ranking->check_tactics_id($this->tactics_id);
			$where = "tactics.tactics_id = $this->tactics_id";
		}

		$this->ranking->check_user_id($this->user_id);
		$keywords = M('keywords'.$this->user_id);
		$tableName = $keywords->getTableName();
		$field = "tactics.tactics_id,
		          tactics.tactics_name,
		          tactics.tactics_status,
		          tactics.executions,
		          tactics.tactics_modified_time,
		          tactics.campaign_period,
		          tactics.premium,
		          tactics.device,
		          tactics.heighest_price,
				  adgroups.num_iid,
				  adgroups.title as adgroups_title,
				  campaigns.title as campaigns_title,
				  `$tableName`.keyword_id,
				  `$tableName`.campaign_id,
				  `$tableName`.adgroup_id,
				  `$tableName`.word,
				  `$tableName`.max_price,
				  `$tableName`.max_mobile_price,
				  want_rank.want_rank,
				  want_rank.msg,
				  want_rank.update_time_keyword,
				  want_rank.original_value,
				  want_rank.doing";

		$this->paging->setRowCount($keywords->join("RIGHT JOIN adgroups ON `$tableName`.adgroup_id = adgroups.adgroup_id")
			             ->join("RIGHT JOIN campaigns ON adgroups.campaign_id = campaigns.campaign_id")
                         ->join("LEFT JOIN want_rank ON `$tableName`.keyword_id = want_rank.keyword_id")
                         ->join("LEFT JOIN tactics ON tactics.tactics_id = want_rank.tactics_id")
                         ->where($where)->count());

		$this->paging->setPageCount(ceil($this->paging->getRowCount()/$this->paging->getPageSize()));

		$ranking_info = $keywords->field($field)->join("RIGHT JOIN adgroups ON `$tableName`.adgroup_id = adgroups.adgroup_id")
		                                         ->join("RIGHT JOIN campaigns ON `$tableName`.campaign_id = campaigns.campaign_id")
						                         ->join("LEFT JOIN want_rank ON `$tableName`.keyword_id = want_rank.keyword_id")
					                             ->join("LEFT JOIN tactics ON tactics.tactics_id = want_rank.tactics_id")
					                             ->where($where)->limit($this->paging->getOffset().','.$this->paging->getPageSize())->select();

	      
	    // dump($keywords->getLastSql());
	    
  //   	//获取实时排名
		// $this->paging->setRes_array($this->ranking->getCurrentRank($ranking_info));

		$this->paging->setRes_array($ranking_info);

    }
}

//得所有的卡位策略为了竞拍
class GetRankExecute extends GetRank
{
	/**
	 * [execute description]
	 * @param  [int]      $user_id    [用户id]
	 * @return [type]                  [策略信息,排除不可执行的,不包含实时排名信息]
	 */
	function __construct($user_id)
	{
		parent::__construct($user_id);
	}

	function execute()
	{
		$this->ranking->check_user_id($this->user_id);
		$where = "tactics.tactics_id != '' AND tactics.tactics_status = 1  AND tactics.user_id = $this->user_id AND want_rank.doing != 4";
		// $where = "tactics.tactics_id != '' AND tactics.tactics_status = 1  AND tactics.user_id = $this->user_id";

		$this->ranking->check_user_id($this->user_id);
		$keywords = M('keywords'.$this->user_id);
		$tableName = $keywords->getTableName();
		$field = "tactics.tactics_id,
		          tactics.tactics_modified_time,
		          tactics.campaign_period,
		          tactics.premium,
		          tactics.heighest_price,
		          tactics.from,
		          tactics.to,
				  adgroups.num_iid,
				  `$tableName`.keyword_id,
				  `$tableName`.campaign_id,
				  `$tableName`.adgroup_id,
				  `$tableName`.word,
				  `$tableName`.max_price,
				  `$tableName`.matchscope,
				  want_rank.want_rank,
				  want_rank.msg,
				  want_rank.update_time_keyword,
				  want_rank.doing";

		$ranking_info = $keywords->field($field)->join("RIGHT JOIN adgroups ON `$tableName`.adgroup_id = adgroups.adgroup_id")
							                         ->join("LEFT JOIN want_rank ON `$tableName`.keyword_id = want_rank.keyword_id")
						                             ->join("LEFT JOIN tactics ON tactics.tactics_id = want_rank.tactics_id")
						                             ->where($where)->select();
	      
	    // dump($keywords->getLastSql());
	    
		return $ranking_info;
	}
}
